package com.bcu.cn.onlineexamsystem.studentmanage.controller;

import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentHomeService;
import com.bcu.cn.onlineexamsystem.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/student/home")
public class StudentHomeController {
    
    @Autowired
    private StudentHomeService studentHomeService;
    
    /**
     * 学生首页
     */
    @GetMapping("/manage")
    public String home(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        // 获取首页数据
        StudentHomeDTO homeData = studentHomeService.getHomeData(user.getUserId());
        
        // 将数据添加到模型中
        model.addAttribute("homeData", homeData);
        
        return "student/home";
    }
    
    /**
     * 获取首页数据的API接口（用于AJAX请求）
     */
    @GetMapping("/data")
    @ResponseBody
    public StudentHomeDTO getHomeData(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return null;
        }
        
        return studentHomeService.getHomeData(user.getUserId());
    }
} 